package com.Impl;

import com.dao.BaseDao;
import com.dao.purchase_recordDao;
import com.entity.purchase_record;
import com.service.purchase_recordService;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class purchase_recordDaoImpl extends BaseDao implements purchase_recordDao {
  public purchase_recordDaoImpl(Connection con) {
    super(con);
  }

  @Override
  public List<purchase_record> findAll() {
    List<purchase_record> list = new ArrayList<>();
    String sql = "select * from purchase_record";
    ResultSet rs = this.executeQuery(sql,null);
    try {
      while(rs.next()){
        purchase_record purchase_record = new purchase_record();
        purchase_record.setPid(rs.getInt(1));
        purchase_record.setSpid(rs.getInt(2));
        purchase_record.setPname(rs.getString(3));
        purchase_record.setPcount(rs.getInt(4));
        purchase_record.setPathPic(rs.getString(5));
        list.add(purchase_record);
      }
    } catch (SQLException throwables) {
      throwables.printStackTrace();
    }
    return list;
  }

  @Override
  public int getCount() {
    int count = 0;
    String sql = "select count(*) from purchase_record";
    ResultSet rs = this.executeQuery(sql,null);
    try {
      while (rs.next()){
        count = rs.getInt(1);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return count;
  }
}
